package com.chenchen.auth.domain.service.impl;


import com.chenchen.auth.common.enums.IsDeletedFlagEnum;
import com.chenchen.auth.domain.entity.AuthRolePermissionBO;
import com.chenchen.auth.domain.service.AuthRolePermissionDomainService;
import com.chenchen.auth.infra.basic.entity.AuthRolePermission;
import com.chenchen.auth.infra.basic.service.AuthRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {

    @Resource
    private AuthRolePermissionService authRolePermissionService;

    @Override
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {
        // 创建一个AuthRolePermission类型的List
        List<AuthRolePermission> rolePermissionList = new LinkedList<>();
        // 获取roleId
        Long roleId = authRolePermissionBO.getRoleId();
        // 遍历permissionIdList
        authRolePermissionBO.getPermissionIdList().forEach(permissionId -> {
            // 创建一个新的AuthRolePermission对象
            AuthRolePermission authRolePermission = new AuthRolePermission();
            // 设置roleId
            authRolePermission.setRoleId(roleId);
            // 设置permissionId
            authRolePermission.setPermissionId(permissionId);
            // 设置isDeleted
            authRolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            // 将AuthRolePermission对象添加到rolePermissionList中
            rolePermissionList.add(authRolePermission);
        });
        // 调用authRolePermissionService的batchInsert方法，插入rolePermissionList
        int count = authRolePermissionService.batchInsert(rolePermissionList);
        // 返回count是否大于0
        return count > 0;
    }


}
